package main

import (
	"fmt"
	"math/rand"
	"sort"
	"strings"
	"time"
)

func a1() {
	s1 := make([]int, 3, 4)
	s2 := append(s1, 1)
	s2[1] = 6
	fmt.Printf("%v %p %p %d %d\n", s1, &s1, &s1[0], len(s1), cap(s1))
	fmt.Printf("%v %p %p %d %d\n", s2, &s2, &s2[0], len(s2), cap(s2))
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
	s2 = append(s2, 2)
	fmt.Printf("%v %p %p %d %d\n", s1, &s1, &s1[0], len(s1), cap(s1))
	fmt.Printf("%v %p %p %d %d\n", s2, &s2, &s2[0], len(s2), cap(s2))
	s2[0] = 5
	fmt.Printf("%v %p %p %d %d\n", s2, &s2, &s2[0], len(s2), cap(s2))
}

func b1() {
	a := [...]int{1, 4, 9, 16, 2, 5, 10, 15}
	fmt.Println(a)
	length := len(a) - 1
	out := make([]int, 0, length)
	for i := 0; i < length; i++ {
		out = append(out, a[i]+a[i+1])
	}
	fmt.Println(out)
}

func c1() {
	total := 100
	counter := make(map[int]int, total)
	keys := make([]int, 0, total)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	nums := make([]int, 0, total)
	for i := 0; i < 100; i++ {
		num := r.Intn(401) - 200
		nums = append(nums, num)
		if _, ok := counter[num]; !ok {
			counter[num] = 1
			keys = append(keys, num)
		}
		counter[num] += 1
	}
	fmt.Println(nums)
	fmt.Println("~~~~~~~~~~~~~~~~~~")
	fmt.Println(counter, len(counter))
	fmt.Println("~~~~~~~~~~~~~~~~~~")
	sort.Sort(sort.Reverse(sort.IntSlice(keys)))
	for _, k := range keys {
		fmt.Println(k, counter[k])
	}

}

func tmpString() {
	// s := "magedu测试字符串"
	// for i, v := range s {
	// 	fmt.Printf("%d, %T: %[2]d %[2]c; %T: %[3]d %[3]c\n", i, s[i], v)
	// }
	s0 := "test123"
	s1 := "测试一波"
	fmt.Printf("%s %p; %s %p\n", s0, &s0, s1, &s1)

	s2 := s0 + "-" + s1
	fmt.Printf("%s %p\n", s2, &s2)

	s3 := strings.Join([]string{s0, s1}, "-")
	fmt.Printf("%s %p\n", s3, &s3)

	s4 := fmt.Sprintf("%s-%s", s0, s1)
	fmt.Printf("%s %p\n", s4, &s4)
}

func main() {
	// a1()
	// b1()
	c1()
	// tmpString()
}
